package com.hoongs.v1.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.hoongs.v1.dto.ProductPageDto;
import com.hoongs.v1.entity.Product;
import com.hoongs.v1.service.ProductService;
import com.hoongs.v1.utils.Result;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/product")
@Validated
public class ProductController {

    @Resource
    private ProductService productService;

    @GetMapping("/selectListByUser")
    public Result selectListByUser(String name, String cate, String status, @RequestHeader("token") String token) {
        return productService.selectListByUser(name, cate, status, token);
    }

    @PostMapping("/add")
    @PreAuthorize("hasAnyAuthority('admin')")
    public Result add(@Validated Product product, BindingResult bindingResult) {
        String res = Result.resultMsg(bindingResult);
        return StringUtils.isNotBlank(res) ? Result.opError(res) : productService.add(product);
    }

    @PostMapping("/remove")
    @PreAuthorize("hasAnyAuthority('admin')")
    public Result remove(String id) {
        return productService.remove(id);
    }

    @PostMapping("/update")
    @PreAuthorize("hasAnyAuthority('admin')")
    public Result update(Product product) {
        return productService.update(product);
    }

    @GetMapping("/selectById")
    public Result selectById(String id) {
        return productService.selectById(id);
    }


    @GetMapping("/selectList")
    public Result selectList(String name, String cate, String status) {
        return productService.selectList(name, cate, status);
    }

    @GetMapping("/selectPageList")
    @PreAuthorize("hasAnyAuthority('admin')")
    public Result selectPageList(ProductPageDto productDto) {
        return productService.selectPageList(productDto);
    }

}
